package cc.linker.steplink.crm.merchant.task;

import cc.linker.steplink.crm.commons.lock.RedisLock;
import cc.linker.steplink.crm.merchant.dao.AssetCenterMapper;
import cc.linker.steplink.crm.merchant.pojo.UserAccountPo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class AccountSummaryStatisticsTask {
    Logger logger = LoggerFactory.getLogger(AccountSummaryStatisticsTask.class);
    @Autowired
    private AssetCenterMapper assetCenterMapper;

    /**
     * 测试接口调用
     *
     * @param day
     */
    public void calcAccountSummary(String day) {
        accountSummary(day);
    }

    @RedisLock("AccountSummaryStatisticsTask_accountSummary")
    @Scheduled(cron = "10 0 0 * * ?")
//    @Scheduled(cron = "0/30 * * * * ?" )
    public void calcAccountSummary() {
        accountSummary(null);
    }

    /**
     * 机构账单每日汇总
     *
     * @param day
     */
    private void accountSummary(String day) {
        try {
            String statisticsTime;
            if (StringUtils.isEmpty(day)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date = new Date();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                date = calendar.getTime();
                statisticsTime = sdf.format(date);
            } else {
                statisticsTime = day;
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("statisticsTime", statisticsTime);
            // 获取所有机构
            //List<UserAccountPo> adminAccounts = assetCenterMapper.getAdminAccounts();
            List<String> orgIds = assetCenterMapper.listOrgcode();
            for (String orgId : orgIds) {
                map.put("orgId", orgId);
                if (assetCenterMapper.ifSummaryStatistics(map) <= 0) {
                    assetCenterMapper.accountSummaryStatistics(map);
                } else {
                    assetCenterMapper.updateSummaryStatistics(map);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试接口调用
     *
     * @param day
     */
    public void calcAccountSummarySaler(String day) {
        accountSummarySaler(day);
    }


    @RedisLock("AccountSummaryStatisticsTask_accountSummarySaler")
    @Scheduled(cron = "15 0 0 * * ?")
    public void calcAccountSummarySaler() {
        accountSummarySaler(null);
    }

    /**
     * 小b账单每日汇总
     *
     * @param day
     */
    private void accountSummarySaler(String day) {
        try {
            String statisticsTime;
            if (StringUtils.isEmpty(day)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date = new Date();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                date = calendar.getTime();
                statisticsTime = sdf.format(date);
            } else {
                statisticsTime = day;
            }
            Map<String, Object> map = new HashMap<>();
            map.put("statisticsTime", statisticsTime);
            // 获取小b用户
            List<Map<String, Object>> salerIds = assetCenterMapper.listSalerIds();
            for (Map<String, Object> saler : salerIds) {
                map.put("orgId", saler.get("orgId"));
                map.put("salerId", saler.get("salerId"));
                if (assetCenterMapper.ifSummaryStatisticsSaler(map) <= 0) {
                    boolean success = assetCenterMapper.accountSummaryStatisticsSaler(map);
                    if (!success) {
                        assetCenterMapper.accountSummaryStatisticsSaler0(map);
                    }
                } else {
                    assetCenterMapper.updateSummaryStatisticsSaler(map);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @RedisLock("AccountSummaryStatisticsTask_updateAccountMoneyDayTasks")
    @Scheduled(cron = "0 1 0 * * ?")
    /**
     * 今天解冻金额打到可用余额中
     *  1、查询交易记录今天解冻记录的人与钱
     *  2、更新交易解冻金额及可用余额
     */
    public void updateAccountMoneyDayTasks() {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            String currentDate = sdf.format(calendar.getTime());
            Map<String, Object> map = new HashMap<>();
            map.put("currentDate", currentDate);
            // 查询交易记录今天解冻记录的人与钱
            List<UserAccountPo> accountList = assetCenterMapper.selectUnfreezeUserList(map);
            if (accountList != null && accountList.size() > 0) {
                //更新用户账单
                for (UserAccountPo userAccountPo : accountList) {
                    assetCenterMapper.updateAccountInfoByUid(userAccountPo);
                }
                //更新账单状态
                assetCenterMapper.updateTradeInfo(map);
            } else {
                logger.debug(currentDate + " 没有需要解冻的金额");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
